package com.jyw.business.ast.controller;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.jyw.business.ast.domain.dto.web.AstConsumableEditDto;
import com.jyw.business.ast.domain.dto.web.AstConsumablePageDto;
import com.jyw.business.ast.domain.dto.web.AstConsumableReverseDto;
import com.jyw.business.ast.domain.dto.web.AstConsumableSignDto;
import com.jyw.business.ast.domain.vo.web.AstConsumableDetailVo;
import com.jyw.business.ast.domain.vo.web.AstConsumablePageVo;
import com.jyw.common.core.domain.CommonResult;
import com.jyw.common.core.page.BasePage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.jyw.common.annotation.Log;
import com.jyw.common.core.controller.BaseController;
import com.jyw.common.core.domain.AjaxResult;
import com.jyw.common.enums.BusinessType;
import com.jyw.business.ast.domain.AstConsumable;
import com.jyw.business.ast.service.IAstConsumableService;
import com.jyw.common.utils.poi.ExcelUtil;
import com.jyw.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 资产-耗材信息Controller
 * 
 * @author system
 * @date 2023-10-24
 */
@RestController
@RequestMapping("/ast/consumable")
@Api(tags = "耗材管理")
public class AstConsumableController extends BaseController
{
    @Autowired
    private IAstConsumableService astConsumableService;

    /**
     * 查询资产-耗材信息列表
     */
    @PreAuthorize("@ss.hasPermi('ast:consumable:list')")
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo<AstConsumablePageVo> list(BasePage page, @RequestBody AstConsumablePageDto dto)
    {
        startPage();
        List<AstConsumablePageVo> list = astConsumableService.consumablePage(dto);
        return getDataTable(list);
    }

    /**
     * 获取资产-耗材信息详细信息
     */
    @PreAuthorize("@ss.hasAnyPermi('ast:consumable:query,ast:consumable:edit')")
    @GetMapping(value = "/{id}")
    @ApiOperation("耗材详情")
    public CommonResult<AstConsumableDetailVo> getInfo(@PathVariable("id") Long id)
    {

        return CommonResult.success(astConsumableService.consumableDetail(id));
    }

    /**
     * 新增资产-耗材信息
     */
    @PreAuthorize("@ss.hasPermi('ast:consumable:add')")
    @Log(title = "资产-耗材信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("耗材登记")
    public CommonResult add(@RequestBody @Valid AstConsumableSignDto dto)
    {
        astConsumableService.consumableSign(dto);
        return CommonResult.success();
    }

    /**
     * 修改资产-耗材信息
     */
    @PreAuthorize("@ss.hasPermi('ast:consumable:edit')")
    @Log(title = "资产-耗材信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("编辑耗材")
    public CommonResult edit(@RequestBody AstConsumableEditDto dto)
    {
        astConsumableService.edit(dto);
        return CommonResult.success();
    }

    /**
     * 修改资产-耗材信息
     */
    @PreAuthorize("@ss.hasPermi('ast:consumable:warehouse')")
    @Log(title = "资产-耗材信息", businessType = BusinessType.UPDATE)
    @PutMapping("in")
    @ApiOperation("耗材入库")
    public CommonResult warehouse(@RequestBody AstConsumableReverseDto dto)
    {
        astConsumableService.warehouse(dto);
        return CommonResult.success();
    }

    /**
     * 修改资产-耗材信息
     */
    @PreAuthorize("@ss.hasPermi('ast:consumable:outOfWarehouse')")
    @Log(title = "资产-耗材信息", businessType = BusinessType.UPDATE)
    @PutMapping("out")
    @ApiOperation("耗材出库")
    public CommonResult outOfWarehouse(@RequestBody AstConsumableReverseDto dto)
    {
        astConsumableService.outOfWarehouse(dto);
        return CommonResult.success();
    }

    /**
     * 删除资产-耗材信息
     */
    @PreAuthorize("@ss.hasPermi('ast:consumable:remove')")
    @Log(title = "资产-耗材信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("耗材删除")
    public CommonResult remove(@PathVariable Long[] ids)
    {
        return CommonResult.success(astConsumableService.deleteAstConsumableByIds(ids));
    }

    /**
     * 下载耗材模板
     */
    @GetMapping("template")
    @ApiOperation("下载耗材模板")
    @PreAuthorize("@ss.hasPermi('ast:consumable:template')")
    public void template(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("耗材模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        IOUtils.copy(new ClassPathResource("template/consumable_import.xlsx").getInputStream(),response.getOutputStream());
        response.flushBuffer();
    }

    /**
     * 下载耗材模板
     */
    @PostMapping("import")
    @ApiOperation("耗材导入")
    @PreAuthorize("@ss.hasPermi('ast:consumable:import')")
    public CommonResult importTemplate(@RequestParam("file") MultipartFile file){
        astConsumableService.importData(file);
        return CommonResult.success();
    }
}
